package com.araaya.eslimsmobile;


import java.io.File;
import java.io.FileNotFoundException;

import com.araaya.eslimsmobile.R;
import com.araaya.eslimsmobile.util.FileBrowser;
import com.araaya.eslimsmobile.util.ElandConstant;
import com.araaya.eslimsmobile.util.KmlParser;
import com.araaya.eslimsmobile.util.PropertyReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ElandMobileHome extends Activity {
	
	private static final int REQUEST_KML_FILE = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eland_home);

		final Button btnKmlMap = (Button) findViewById(R.id.btnKmlMap);
		final Button btnWebDetail = (Button) findViewById(R.id.btnWebDetail);
		final Button btnQuit = (Button) findViewById(R.id.btnQuit);

		// KmlMap Button -- genarate kml file
		btnKmlMap.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getBaseContext(), KmlHome.class);
				i.putExtra("web",false);
				startActivity(i);
			}
		});

		// Web Detail Button -- Add a new Detail Land
		btnWebDetail.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getBaseContext(), RunWebSite.class);
				startActivity(i);
				
				/*Intent i = new Intent(getBaseContext(), WebSiteCaller.class);
				i.putExtra("webAddress",ElandConstant.MAIN_SITE_URL);
				startActivity(i);*/

			}
		});

		// Quit Button
		btnQuit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				exitApplication();
				/*Intent i = new Intent(getBaseContext(), ImageUpload.class);
				startActivity(i);*/
			}
		});
		
		//Toast.makeText(getBaseContext(), "Server IP : "+ ElandConstant.IP, Toast.LENGTH_LONG).show();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			exitApplication();
			return true;

		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void exitApplication() {
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage(ElandConstant.MAIN_EXIT_STR)
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								
								System.exit(0);

							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// Action for 'NO' Button
						dialog.cancel();
					}
				});
		AlertDialog alert = alt_bld.create();
		alert.setTitle("eSLIMS Exit");
		alert.setIcon(R.drawable.close);
		alert.show();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.home_menu, menu);

		return super.onCreateOptionsMenu(menu);

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuWebsite:
			Intent i = new Intent(getBaseContext(), WebSiteCaller.class);
			//Log.d("eSlims", "Site_URL : "+ ElandConstant.SITE_URL);
			//Toast.makeText(ElandMobileHome.this, "Site_URL : "+ ElandConstant.SITE_URL , Toast.LENGTH_LONG).show();
			String site_url;
			try{
				site_url = PropertyReader.loadProperties();
			}catch (FileNotFoundException e){
				site_url =  ElandConstant.MAIN_SITE_URL;
			}
			
			i.putExtra("webAddress",site_url);
			startActivity(i);
			//finish();
			return true;

		case R.id.menuFinish:
			exitApplication();
			return true;
			
		case R.id.menuImport:
			Intent intent = new Intent(getBaseContext(), FileBrowser.class);
			startActivityForResult(intent, REQUEST_KML_FILE);
		}
		return false;
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK && requestCode == REQUEST_KML_FILE){
			if(data.hasExtra("KML_File")){
				File kml_file = (File) data.getExtras().getSerializable("KML_File");
				if(kml_file.isFile()){
					boolean status = new KmlParser().parseKmlToDatabase(kml_file, ElandMobileHome.this);
					if(status){
						Toast.makeText(ElandMobileHome.this, "KML Import Sucessful", Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(ElandMobileHome.this, "KML Import Error", Toast.LENGTH_LONG).show();
					}
				}
			}
		}
	}


}